import React, { useState, useEffect } from 'react'
import { Col, Form, Input, Button, Select, Table, } from 'antd'
import { PageHeaderWrapper } from '@ant-design/pro-layout'
import RenderButtom from '@/components/RenderButton'
import SearchBox from '@/components/SearchBox'
import moment from 'moment'
import { connect } from 'dva'
import {
  getPayrollList,
} from '@/services/distribution/commission'

const hasSearchBtn = JSON.parse(localStorage.getItem('hasSearchBtn'))
const columns = [
  {
    title: '序号',
    dataIndex: 'sort',
    key: 'sort',
    render(text, record, index){
      return <div>{index + 1}</div>
    }
  },
  {
    title: '商户名称',
    dataIndex: 'merchant_name',
    key: 'merchant_name',
  },
  {
    title: '门店名称',
    dataIndex: 'name',
    key: 'name',
  },
  {
    title: '余额',
    dataIndex: 'payments_balance',
    key: 'payments_balance',
  },
];

const Payroll = props => {
  const { form, merchants, stores, dispatch, } = props
  const { getFieldDecorator } = form
  const [data, setData] = useState([])
  const [total, setTotalPage] = useState(null)
  const [param, setParam] = useState({
    pageNo: 1,
    pageSize: 10,
  })
  useEffect(() => {
    init()
    dispatch({
      type: 'distribute/fetchMerchants'
    })
  }, [])

  const init = async (params = param) => {
    const res = await getPayrollList(params)
    setData(res.data)
    setTotalPage(res.total)
  }

  // 表单查询
  const handleSearch = e => {
    e.preventDefault()
    form.validateFields((err, values) => {
      console.log('Received values of form: ', values)
      init({
        ...values,
      })
    })
  }
  // 表单清空
  const handleReset = e => {
    form.resetFields();
    init()
  }
  // 查询条件
  const getFields = () => {
    return(
      <>
        <Col span={8}>
          <Form.Item label={`所属商户`}>
            {getFieldDecorator(`supplierId`, {})
              (<Select onChange={selectMerchant}>
                {
                  merchants && merchants.map(m => <Select.Option key={m.id} value={m.id}>{m.merchantName}</Select.Option>)
                }
              </Select>)}
          </Form.Item>
        </Col>
        <Col span={8}>
          <Form.Item label={`门店`}>
            {getFieldDecorator(`storeId`, {})
              (<Select>
                {
                  stores && stores.map(m => <Select.Option key={m.id} value={m.id}>{m.name}</Select.Option>)
                }
              </Select>)}
          </Form.Item>
        </Col>
      </>
    )
  }

  const selectMerchant = (val) => {
    dispatch({
      type: 'distribute/fetchStores',
      payload: {
        supplierId: val,
        pageSize: 999,
      }
    })
  }

  // 按钮处理
  const handleBtn = type => {
    
  }

  const pagination = {
    total: parseInt(total),
    onChange(page) {
      init({
        pageNo: page,
      })
    }
  }  

  return (
    <PageHeaderWrapper>
      <SearchBox handleReset={handleReset} handleSearch={handleSearch} getFields={getFields} />
      <RenderButtom click={handleBtn} />
      <Table 
        columns={columns}
        dataSource={hasSearchBtn ? data : []}
        rowKey={record => record.merchant_name}
        pagination={pagination}
      />
    </PageHeaderWrapper>
    )
}

export default connect(({ distribute }) => ({
  merchants: distribute.merchants,
  stores: distribute.stores,
}))(Form.create()(Payroll))
